package logic

import (
	"api/models/mysql"
	"api/models/redisinit"
	"context"
	"usersrv/internal/svc"
	"usersrv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginUserLogic {
	return &LoginUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginUserLogic) LoginUser(in *user.LoginUserReq) (*user.LoginUserResp, error) {
	// todo: add your logic here and delete this line
	var FindUser mysql.Users

	err := FindUser.GetInfoMobile(in.Mobile)
	if err != nil {
		logx.Errorf("用户不存在")
		return nil, err
	}
	get := redisinit.ClientRedis.Get("sendsmslogin" + in.Mobile)
	if get.Val() != in.SendSms {
		logx.Errorf("短信验证码错误")
	}

	if FindUser.ID != 0 {
		FindUser.Mobile = in.Mobile

		err = FindUser.GetCreates()
		if err != nil {
			logx.Error("用户添加失败")
			return nil, err
		}
	}

	return &user.LoginUserResp{Id: int64(FindUser.ID)}, nil
}
